package domain;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.PrintWriter;
import java.net.ConnectException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 *
 * @author Joachim
 */
public class Client {
    
    // Server settings
    private static InetAddress host;
    private final int PORT = 1337;
    
    // Send things to the server
    private PrintWriter printWriter;
    private BufferedOutputStream bufferedOutputStream;
            
    // Receive things from the server
    private ObjectInputStream objectInputStream;
    
    public Client() throws ConnectException {
        initHost();
        initConnection();
    }
    
    private void initHost() {
        try {
            host = InetAddress.getLocalHost();
        } catch(UnknownHostException e) {
            System.out.println("Host id not found!");
            System.exit(1);
        }
    }
    
    private void initConnection() throws ConnectException {
        Socket link = null;
        try {
            link = new Socket(host,PORT);
            printWriter = new PrintWriter(link.getOutputStream(), true);            
            bufferedOutputStream = new BufferedOutputStream(link.getOutputStream());
            objectInputStream = new ObjectInputStream(link.getInputStream());
        } catch(IOException e) {
            throw new ConnectException("Connection failed");
        }
    }
    
    public boolean logIn(String email, String wachtwoord) throws ConnectException {       
        Boolean loginSuccess = false;        
        try {
            printWriter.println(email);
            printWriter.println(wachtwoord);
            loginSuccess = (Boolean) objectInputStream.readObject();  
        } catch(ClassNotFoundException e) {
            e.printStackTrace();
        } catch(IOException e) {
            throw new ConnectException("Connection failed");
        }       
        return loginSuccess;
    }
    
    public void uploadFile(String url) {
        File file = new File(url);
        byte[] fileByteArray = new byte[(int) file.length()];
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(file);
        } catch (FileNotFoundException ex) {
            // Do exception handling
        }
        BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
        try {
            bufferedInputStream.read(fileByteArray, 0, fileByteArray.length);
            bufferedOutputStream.write(fileByteArray, 0, fileByteArray.length);
            bufferedOutputStream.flush();
        } catch (IOException ex) {
            // Do exception handling
        }
    }
    
}
